﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Visual_Studio_Test.ExpandoObject
{
	/// <summary>
	/// The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members
	/// The ExpandoObject class is an implementation of the dynamic object concept
	/// </summary>
	class Example
	{
		static public void Run()
		{
			dynamic sampleObject = new System.Dynamic.ExpandoObject();

			Console.WriteLine("Dynamic property example");
			sampleObject.test = "Dynamic Property";
			Console.WriteLine(sampleObject.test);
			Console.WriteLine(sampleObject.test.GetType());

			Console.WriteLine(Environment.NewLine);
			Console.WriteLine("Dynamic method example");
			sampleObject.number = 10;
			sampleObject.Increment = (Action)(() => { sampleObject.number++; });
			sampleObject.Add = (Action<int>)((ammount) => { sampleObject.number += ammount; });
			// Before calling the Increment method.
			Console.WriteLine(sampleObject.number);
			sampleObject.Increment();
			// After calling the Increment method.
			Console.WriteLine(sampleObject.number);
			sampleObject.number = 1;
			sampleObject.Add(2);
			// After calling the Add method.
			Console.WriteLine(sampleObject.number);

			Console.WriteLine(Environment.NewLine);
			Console.WriteLine("Dynamic event example");
			// Create a new event and initialize it with null.
			sampleObject.sampleEvent = null;
			// Add an event handler.
			sampleObject.sampleEvent += new EventHandler(SampleHandler);
			// Raise an event for testing purposes.
			sampleObject.sampleEvent(sampleObject, new EventArgs());

			//More examples
			PassingDynamicValueAsParameterExample();
			EnumeratingAndDeletingMembersExample();
			NotificationOfPropertyChangesExample();

		}


		// Event handler.
		static void SampleHandler(object sender, EventArgs e)
		{
			Console.WriteLine("SampleHandler for {0} event", sender);
		}

		static private void PassingDynamicValueAsParameterExample()
		{
			dynamic employee, manager;

			employee = new System.Dynamic.ExpandoObject();
			employee.Name = "John Smith";
			employee.Age = 33;

			manager = new System.Dynamic.ExpandoObject();
			manager.Name = "Allison Brown";
			manager.Age = 42;
			manager.TeamSize = 10;

			WritePerson(manager);
			WritePerson(employee);
		}

		static private void WritePerson(dynamic person)
		{
			Console.WriteLine("{0} is {1} years old.",
							  person.Name, person.Age);
			// The following statement causes an exception
			// if you pass the employee object.
			// Console.WriteLine("Manages {0} people", person.TeamSize);
		}

		static private void EnumeratingAndDeletingMembersExample()
		{
			dynamic employee = new System.Dynamic.ExpandoObject();
			employee.Name = "John Smith";
			employee.Age = 33;

			Console.WriteLine("Enumerating members");
			//The ExpandoObject class implements the IDictionary<String, Object> interface
			foreach (var property in (IDictionary<String, Object>)employee)
			{
				Console.WriteLine(property.Key + ": " + property.Value);
			}

			Console.WriteLine(Environment.NewLine);
			Console.WriteLine("Deleting members");
			Console.WriteLine(string.Format("employee name: {0}", employee.Name));
			((IDictionary<String, Object>)employee).Remove("Name");
			try
			{
				Console.WriteLine(string.Format("employee name: {0}", employee.Name));
			}
			catch (Exception e)
			{
				Console.WriteLine(string.Format("employee name: no longer exists"));
			}
		}

		static private void NotificationOfPropertyChangesExample()
		{
			dynamic employee = new System.Dynamic.ExpandoObject();
			((INotifyPropertyChanged)employee).PropertyChanged += new PropertyChangedEventHandler(HandlePropertyChanges);
			employee.Name = "John Smith";
		}

		static private void HandlePropertyChanges(object sender, PropertyChangedEventArgs e)
		{
			Console.WriteLine("{0} has changed.", e.PropertyName);
		}
	}
}
